package com.css.apps.base.common.provider;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.css.apps.base.org.model.SUserOrg;
import com.css.apps.base.user.model.SUser;
import com.css.db.query.QueryCache;

/**
 * 关联实体查询集合
 * @author liuhy 2019 2019年3月17日
 *
 */
public class ReleateQueryProvider {

	@SuppressWarnings("unchecked")
	public static List<SUserOrg> listUserOrg () {
		List<SUserOrg> res = new QueryCache("from SUserOrg").list();
		return res;
	}
	
	@SuppressWarnings("unchecked")
	public static Map<String, List<String>> groupUserIdsByDeptId () {
		Map<String, List<String>> res = new HashMap<>();
		List<SUserOrg> userorgList = new QueryCache("from SUserOrg").list();
		for (SUserOrg uo : userorgList) {
			String deptId = uo.getDeptId();
			List<String> userIds = res.get(deptId);
			if (userIds == null) {
				userIds = new ArrayList<>();
			}
			userIds.add(uo.getUserId());
			res.put(deptId, userIds);
		}
		return res;
	}
	
	@SuppressWarnings("unchecked")
	public static Map<String, List<SUser>> groupUsersByDeptId() {
		Map<String, List<SUser>> res = new HashMap<>();
		Map<String, List<String>> userorgIdMap = groupUserIdsByDeptId();
		Set<String> deptIds = userorgIdMap.keySet();
		for (String deptId : deptIds) {
			List<SUser> users = QueryCache.idToObj(SUser.class, userorgIdMap.get(deptId));
			res.put(deptId, users);
		}
		return res;
	}
	
}
